//
//  DHCycleTableViewCell.m
//  YDAPP
//
//  Created by tyxd on 17/2/27.
//  Copyright © 2017年 TYXD. All rights reserved.
//

#import "DHCycleTableViewCell.h"

CGFloat maxContentLabelHeight = 0;  //根据具体font而定
const CGFloat btnHeight = 20;
@interface DHCycleTableViewCell()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    //赞的高度
    CGFloat _firstH;
    //评论高度
    CGFloat _secondH;
    
    
    //点赞与评论的分割线
    UIView *_LCSpeView;
    
    //赞的心👍
    UIImageView *_zanImage;
    
    //我的号码
    NSString *_myTel;
    
    //记录赞面板的
//    BOOL _hid;
}

//数据源
@property(nonatomic,strong)NSMutableArray *dataArr;

@property(nonatomic,strong)NSMutableArray *chatDataArrM;

@property(nonatomic,weak)UITableView *tableView;

@end
@implementation DHCycleTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    
    //我的号码
    _myTel =  [[NSUserDefaults standardUserDefaults]objectForKey:@"TelNum"];
    
    //头像
    self.iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.iconBtn.layer.cornerRadius = 25 ;
    self.iconBtn.clipsToBounds = YES;
    [self.iconBtn setImage:PlaceHold forState:UIControlStateNormal];
    
//    [self.iconBtn addTarget:self action:@selector(contact) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.iconBtn];
    
    //昵称
    self.nameLable = [UILabel new];
    self.nameLable.font = ProjectMidFont;
    self.nameLable.text = @"陈志强";
    self.nameLable.textColor = BlackColor;
    [self.contentView addSubview:self.nameLable];
    
    //性别
    UIButton *sexBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sexBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
    [sexBtn setImage:@"icon_women"];
    [sexBtn setTitle:@"20 "];
    sexBtn.hidden = YES;
    [sexBtn setTitleColor:[UIColor redColor]];
    sexBtn.titleLabel.font = SmallFont;
    [self.contentView addSubview:sexBtn];
    self.sexBtn = sexBtn;
    
    //举报
    UIButton *reportBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [reportBtn setImage:@"icon_more_c"];
    reportBtn.hidden = YES;
    [self.contentView addSubview:reportBtn];
    self.reportBtn = reportBtn;
    
    //发布时间
    self.timeLable = [UILabel new];
    self.timeLable.font = SmallFont;
    self.timeLable.text = @"03-22 17:15";
    self.timeLable.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:self.timeLable];
    
    //所在场馆
    UIButton *clubNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    clubNameBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
    clubNameBtn.layer.cornerRadius = 2;
    clubNameBtn.clipsToBounds = YES;
    [clubNameBtn setTitle:@"华武决搏击健身俱乐部"];
    clubNameBtn.backgroundColor = ButtonBackGroundColor;
    [clubNameBtn setTitleColor:BlackColor];
    clubNameBtn.titleLabel.font = SmallFont;
    [self.contentView addSubview:clubNameBtn];
    self.clubNameBtn = clubNameBtn;
    
    
    
    //圈子内容
    self.contentLable = [UILabel new];
    self.contentLable.font = MidBigFont;
    self.contentLable.text = @"陈志强nsfjsdkfjs";
    self.contentLable.textColor = BlackColor;
    self.contentLable.numberOfLines = 0;
    [self.contentView addSubview:self.contentLable];
    
    //带链接的直播等
//    self.urlView = [UIView new];
//
//    self.urlView.backgroundColor = BackGroundColor;
//
//    [self.contentView addSubview:self.urlView];
//
//    //链接图片内容
//    self.urlImageView = [UIImageView new];
//
//    self.urlImageView.image = [UIImage imageNamed:@"bg"];
//
//    [self.urlView addSubview:self.urlImageView];
//
//    self.urlContentLable = [UILabel new];
//
//    self.urlContentLable.font = [UIFont systemFontOfSize:14];
//
//    self.urlContentLable.text = @"正在直播，大家赶紧围观";
//
//    self.urlContentLable.textColor = [Helper colorWithHexString:@"#454545"];
//
//    self.urlContentLable.numberOfLines = 0;
//
//    [self.urlView addSubview:self.urlContentLable];
    
    
    //更多内容
    self.moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.moreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    [self.moreBtn setTitle:@"全文" forState:UIControlStateNormal];
    [self.moreBtn setTitleColor:BackGroundColor forState:UIControlStateNormal];
    self.moreBtn.titleLabel.font = MidFont;
    [self.moreBtn addTarget:self action:@selector(open) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.moreBtn];
    
    //放图片面板
//    self.photoView = [[YHWorkGroupPhotoContainer alloc] initWithWidth:kScreenWidth-75];
//    [self.contentView addSubview:self.photoView];
    
    self.jggView = [JGGView new];
    [self.contentView addSubview:self.jggView];
    
    //观看人数
    UIButton *seeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    seeBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
    [seeBtn setImage:@"icon_browse"];
    [seeBtn setTitle:@"0 "];
    [seeBtn setTitleColor:GrakColor];
    seeBtn.titleLabel.font = SmallFont;
    [self.contentView addSubview:seeBtn];
    self.seeBtn = seeBtn;
    
    //评论人数
    UIButton *talkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    talkBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
    [talkBtn setImage:@"icon_comment"];
    [talkBtn setTitle:@"3 "];
    [talkBtn setTitleColor:GrakColor];
    talkBtn.titleLabel.font = SmallFont;
    [self.contentView addSubview:talkBtn];
    self.talkBtn = talkBtn;
    
    //点赞人数
    UIButton *goodBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goodBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
    [goodBtn setImage:@"icon_fabulous"];
//    [goodBtn setImage:@"icon_likes_red" forState:UIControlStateSelected];
    [goodBtn setTitle:@"3 "];
    [goodBtn addTarget:self action:@selector(like:)];
    [goodBtn setTitleColor:GrakColor];
    goodBtn.titleLabel.font = SmallFont;
    [self.contentView addSubview:goodBtn];
    self.goodBtn = goodBtn;
    
    //自己发布有删除按钮
    self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [self.deleteBtn setTitleColor:BackGroundColor forState:UIControlStateNormal];
    self.deleteBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_deleteBtn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.deleteBtn];
    
    
    
//    //弹出评论点赞按钮
//    self.upBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.upBtn setImage:[UIImage imageNamed:@"icon_coment"] forState:UIControlStateNormal];
//    self.upBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 6, 3, 0);
//    [self.upBtn addTarget:self action:@selector(zan:) forControlEvents:UIControlEventTouchUpInside];
//    [self.contentView addSubview:self.upBtn];
//
//    //评论点赞面板
//    self.likeView = [UIView new];
//    self.likeView.layer.cornerRadius = 5;
//    self.likeView.clipsToBounds = YES;
//    _likeView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1];
//    [self.contentView addSubview:self.likeView];
//
//    //展现评论内容点赞的人面板
//    self.likeCountView = [UIView new];
//    _likeCountView.backgroundColor = BackGroundColor;
//    [self.contentView addSubview:self.likeCountView];
    
    
    [self layoutUI];

}

-(void)layoutUI
{
    WS(weakSelf);
    [_iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.contentView.mas_top).offset(15);
        make.left.mas_equalTo(weakSelf.contentView.mas_left).offset(15);
        make.height.width.mas_equalTo(50);
        
    }];
    
    [_nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.contentView.mas_top).offset(15);
        make.left.mas_equalTo(weakSelf.iconBtn.mas_right).offset(10);
        make.height.mas_equalTo(20);
    }];
    
    [self.sexBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.nameLable);
        make.left.mas_equalTo(self.nameLable.mas_right).offset(10);
        
    }];
    
    [self.sexBtn imageOnTheTitleLeftWithSpace:3];
    
    [self.reportBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.nameLable);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);
        
    }];
    
    [self.timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.nameLable.mas_bottom).offset(10);
        make.left.mas_equalTo(self.nameLable);
        make.height.mas_equalTo(20);
    }];
    
    [self.clubNameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.timeLable);
        make.left.mas_equalTo(self.timeLable.mas_right).offset(5);
    }];
    
    
    [_contentLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.iconBtn.mas_bottom).offset(10);
        make.left.mas_equalTo(weakSelf.nameLable);
        make.right.mas_equalTo(weakSelf.contentView).offset(-10);
        
    }];
    
    // 不然在6/6plus上就不准确了
//    self.contentLable.preferredMaxLayoutWidth = kScreenWidth - 20-10;
    
    //更多内容
    [_moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.contentLable.mas_bottom).offset(5);
        make.left.mas_equalTo(weakSelf.nameLable);
        
        make.height.mas_equalTo(0);
//        make.width.mas_equalTo(60);
        
        
    }];
    
//    //带链接的直播等
//    [_urlView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.equalTo(weakSelf.moreBtn.mas_bottom).offset(5);
//        make.left.equalTo(weakSelf.iconBtn.mas_right).offset(10);
//        make.right.equalTo(weakSelf.contentView.mas_right).offset(-10);
//
//        make.height.mas_equalTo(0);
//
//
//    }];
//
//    [_urlImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.equalTo(_urlView.mas_top).offset(5);
//        make.left.equalTo(_urlView.mas_left).offset(5);
//        make.bottom.equalTo(_urlView.mas_bottom).offset(-5);
//        make.width.mas_equalTo(40);
//
//
//    }];
//
//    [_urlContentLable mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.equalTo(_urlView.mas_top).offset(5);
//        make.left.equalTo(_urlImageView.mas_right).offset(5);
//        make.bottom.equalTo(_urlView.mas_bottom).offset(-5);
//        make.right.equalTo(_urlView.mas_right).offset(-5);
//
//    }];
//
//    //连接图片

    //放图片面板

    [self.jggView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.moreBtn.mas_bottom).offset(5);
        make.left.mas_equalTo(weakSelf.nameLable);
        

        
    }];
//    [self.jggView setContentHuggingPriority:249 forAxis:UILayoutConstraintAxisVertical];
//    [self.jggView setContentCompressionResistancePriority:749 forAxis:UILayoutConstraintAxisVertical];
    
    
    //观看人数
    [self.seeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.nameLable);
        make.top.mas_equalTo(self.jggView.mas_bottom).offset(10);
        make.bottom.mas_equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(30);
        
    }];
    
    [self.seeBtn imageOnTheTitleLeftWithSpace:3];
    
    
    //自己发布有删除按钮
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.seeBtn);
        make.left.mas_equalTo(weakSelf.seeBtn.mas_right).offset(10);
        make.height.mas_equalTo(0);
        
    }];
    
    //评论人数
    [self.talkBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.seeBtn);
        make.right.mas_equalTo(self.goodBtn.mas_left).offset(-10);
        make.height.mas_equalTo(30);
        
    }];
    
    [self.talkBtn imageOnTheTitleLeftWithSpace:3];
    
    //点赞人数
    [self.goodBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.centerY.mas_equalTo(self.seeBtn);
        make.height.mas_equalTo(30);

        
    }];
    
    [self.goodBtn imageOnTheTitleLeftWithSpace:3];
    

    

}



#pragma mark -- click

#pragma mark -- 点赞
-(void)like:(UIButton *)sender
{
//    sender.selected = !sender.selected;
    
    if (_delegate && [_delegate respondsToSelector: @selector(cycleTableViewCellWithLikeAndDislike:)]) {
        
        [_delegate cycleTableViewCellWithLikeAndDislike:self];
    }
    
}






#pragma mark -- 懒加载

-(NSMutableArray *)chatDataArrM
{
    if (!_chatDataArrM) {
        _chatDataArrM = [NSMutableArray array];
    }
    return _chatDataArrM;
}


-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(void)setCycleModel:(CZQCycleModel *)cycleModel
{
    
    _cycleModel = cycleModel;
    
    [self.iconBtn sd_setImageWithURL:[NSURL URLWithString:[Helper imageUrlStringToChinese:cycleModel.Pic imageType:nil]] forState:UIControlStateNormal placeholderImage:PlaceHold];
    
    self.nameLable.text = cycleModel.Name;
    
    self.timeLable.text = cycleModel.PostTime;
    
    [self.clubNameBtn setTitle:cycleModel.ShopName];
    
    self.clubNameBtn.hidden =  cycleModel.ShopName.length > 0?NO:YES;
    
    self.contentLable.text = cycleModel.Title;
    
    [self.sexBtn setImage: [cycleModel.Sex intValue] == 0?@"icon_man":@"icon_women"];
    
    [self.goodBtn setImage:[cycleModel.IsVote integerValue] == 1 ?@"icon_likes_red":@"icon_fabulous"];
    
    NSLog( @"%@",cycleModel.Pics);
    
    if (cycleModel.Pics.length > 0) {
        
        NSArray *picArr = [cycleModel.Pics componentsSeparatedByString:@","];
        ///布局九宫格
        [self.jggView JGGView:self.jggView DataSource:picArr completeBlock:^(NSInteger index, NSArray *dataSource,NSIndexPath *indexpath) {



        }];
    }
    
    
    
    [self.sexBtn setTitle:[NSString stringWithFormat:@"%@ ",cycleModel.ViewCount]];
    
    [self.talkBtn setTitle:[NSString stringWithFormat:@"%@ ",cycleModel.CommentCount]];
    
    [self.goodBtn setTitle:[NSString stringWithFormat:@"%@ ",cycleModel.VoteCount]];
    
    ///布局九宫格
//    [self.jggView JGGView:self.jggView DataSource:@[PlaceHold,PlaceHold,PlaceHold,PlaceHold,PlaceHold,PlaceHold,PlaceHold,PlaceHold] completeBlock:^(NSInteger index, NSArray *dataSource,NSIndexPath *indexpath) {
//
//
//
//    }];

    
    [self updateConstraints];
    [self setNeedsLayout];
    
    
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
